package com.tesco.controller;


import com.tesco.entity.domin.YgGoods;
import com.tesco.service.YgGoodsService;
import com.tesco.utils.TokenUtils;
import com.tesco.utils.WebApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * @author lx201
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品模块")
@CrossOrigin(origins = "*")
public class GoodsController {

    @Resource
    private YgGoodsService ygGoodsService;

    /**
     * 普通用户模块：获取商品列表
     * @param ygGoods
     * @return
     */
    @GetMapping("/getUserGoodsList")
    @ApiOperation("用户：商品列表")
    public WebApiResult getUserGoodsList(YgGoods ygGoods){
        try {
            List<YgGoods> list = ygGoodsService.getUserGoodsList(ygGoods);
            return WebApiResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return WebApiResult.error();
        }
    }

    /**
     * 商品详情
     * @param id
     * @return
     */
    @GetMapping("/getGoodsDetail")
    @ApiOperation("商品详情")
    public WebApiResult getGoodsDetail(@RequestParam("id") Integer id){
        try {
            List<YgGoods> list = ygGoodsService.getGoodsDetail(id);
            return WebApiResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return WebApiResult.error();
        }
    }


    /**
     * 获取商家的库存信息
     * @param request
     * @return
     */
    @GetMapping("/getStockByMerchant")
    @ApiOperation("获取商家的库存信息(token)")
    public WebApiResult getStockByMerchant(HttpServletRequest request){
        try {
            String username = TokenUtils.getUsername(request);
            List<YgGoods> list = ygGoodsService.getStockByMerchant(username);
            return WebApiResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return WebApiResult.error();
        }
    }


    /**
     * 新增商品
     * @param request
     * @param ygGoods
     * @return
     */
    @PostMapping("/addGoods")
    @ApiOperation("新增商品(token)")
    public WebApiResult addGoods(HttpServletRequest request,YgGoods ygGoods){
        try {
            String username = TokenUtils.getUsername(request);
            ygGoods.setSjname(username);
            ygGoods.setCreatedat(new Date());
            ygGoodsService.save(ygGoods);
            return WebApiResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return WebApiResult.error();
        }
    }


    @PostMapping("/updateStock")
    @ApiOperation("修改商品库存")
    public WebApiResult updateStock(YgGoods ygGoods){
        try {
            ygGoodsService.updateStock(ygGoods);
            return WebApiResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return WebApiResult.error();
        }
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @GetMapping("/deleteStock")
    @ApiOperation("删除商品")
    public WebApiResult deleteStock(@ApiParam(name = "id",value = "商品id")@RequestParam("id") Integer id){
        try {
            ygGoodsService.removeById(id);
            return WebApiResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return WebApiResult.error();
        }
    }

}